%Aug 16: Uses external loss_LR function.
% Generating Data 2 gaussian clusters (2*N points with N positivs and N negatives)
% splitting into 2 parts: training will contain 2*Ntrain examples.
clc;
clear all;
close all;

N = 500;
Y = [ones(N,1); -ones(N,1)];                                                % label 2N LENGTH
H = [Y/2 + randn(2*N,1)/1.5  Y/2-randn(2*N,1)/1.5];                         % data

% doing logistic regression : Prof. Rudin's plugin.
lambda_GLMFIT = glmfit(H(:,1:2), [0.5*Y(:)+0.5 ones(length(Y(:)),1)], 'binomial', 'link', 'logit');

H = [H ones(length(Y),1)];

pos_ind = find(Y == 1);
neg_ind = find(Y == -1);
Hpos = H(pos_ind,:);
Ypos = Y(pos_ind,:);
Hneg = H(neg_ind,:);
Yneg = Y(neg_ind,:);

figure; plot(Hpos(:,1),Hpos(:,2),'b.'); hold on;
plot(Hneg(:,1),Hneg(:,2),'g.'); hold off;


% Logistic Regression using fminsearch
global Hpos Hneg;
opts = optimset('display', 'iter','TolFun',1e-9, 'MaxIter', 500,'MaxFunEvals',10000, 'TolX',1e-6);
lambda0=zeros(size(H,2),1);
[lambda_LR,fval_LR,exitflag,output_LR] = fminsearch(@loss_LR,lambda0,opts)